﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zen4SyncPOCO.POCO.Admin;
using System.Xml.Linq;
using System.IO;
using Zen4SyncPOCO.POCO.Scenario;
using Zen4SyncPOCO.POCO.SQLCEExecution;
using Zen4SyncPOCO.POCO.SQLServerExecution;
using Zen4SyncPOCO.POCO.Enum.Scenario;
using Zen4SyncPOCO.Enum.Admin;
using Zen4SyncPOCO.POCO.SQLCEBackup;
using Zen4SyncPOCO.POCO.MEF;
using Zen4SyncPOCO.POCO.MergeSync;
using Zen4SyncPOCO.Enum.MergeSync;

namespace Zen4SyncPOCO.Test
{
    internal class ClientSessionProvider
    {
        private string testDeploymentDirectory;

        public ClientSessionProvider(string testDeploymentDirectory)
        {
            this.testDeploymentDirectory = testDeploymentDirectory;
        }

        /// <summary>
        /// The ClientSession corresponding to the ClientTestSessionDescriptor.xml content.
        /// </summary>
        internal ClientSession ClientSession
        {
            get
            {
                return new ClientSession()
                {
                    clientSession_id = new Guid("c5bc0460-75d4-4de8-af18-d87cb4a70c62"),
                    clientSession_testServerId = new Guid("16eaa41e-e9da-4a2b-8a92-767bd2cca9fa"),
                    clientSession_testSessionId = new Guid("44accb76-49cc-4b14-baf0-34635fd6f5ea"),
                    clientSession_enableLog = true,
                    clientSession_scheduledStartDateTime = new DateTime(2011, 04, 11, 16, 00, 00, DateTimeKind.Utc),
                    clientSession_testScenario = TestScenario,
                    clientSession_activityContexts = Contexts
                };
            }
        }

        private TestScenario TestScenario
        {
            get
            {
                return new TestScenario()
                {
                    testScenario_id = new Guid("b80f5358-c44f-4a2b-a2bd-bad5f79b5a0e"),
                    testScenario_activities = Activities
                };
            }
        }

        /// <summary>
        /// A list of Activity containing all the activities present in the ClientTestSessionDescriptor.xml file.
        /// </summary>
        private List<Activity> Activities
        {
            get
            {
                List<Activity> activities = new List<Activity>();
                Activity activity = null;
    
                /*************************************
                * SqlServerExecution
                ************************************/
                SqlServerExecution sqlServerExecution = new SqlServerExecution()
                {
                    sqlServerExecution_id = new Guid("228f7430-4f5a-4436-a7e9-a31671128944"),
                    sqlServerExecution_sqlQuery = "UPDATE Customer SET Name = @Name WHERE Id = @Id ;"
                };

                activity = new Activity()
                {
                    activity_id = new Guid("aac891bb-0f67-4b55-b108-37985e4dfb8d"),
                    activity_executionOrder = 1,
                    activity_preWaitingTimeInSeconds = 1,
                    activity_postWaitingTimeInSeconds = 3,
                    activity_sqlServerExecution = sqlServerExecution,
                    activity_activityTypeEnum = ActivityTypeEnum.SqlServerExecution
                };

                activities.Add(activity);

                /*************************************
                * SqlCeExecution
                ************************************/
                SqlceExecution sqlCeExecution = new SqlceExecution
                {
                    sqlceExecution_id = new Guid("1dfb5818-6ec0-437e-9528-b85dd5b3e1dc"),
                    sqlceExecution_sqlQuery = "INSERT INTO Customer (Name) VALUES (@Name) ;"
                };

                activity = new Activity
                {
                    activity_id = new Guid("ffb1a3d4-f5a7-41b1-8006-c7477c835c36"),
                    activity_executionOrder = 2,
                    activity_preWaitingTimeInSeconds = 1,
                    activity_postWaitingTimeInSeconds = 1,
                    activity_activityTypeEnum = ActivityTypeEnum.SqlCeExecution,
                    activity_sqlCeExecution = sqlCeExecution
                };

                activities.Add(activity);

                /*************************************
                * SqlCeBackup
                ************************************/
                SqlceBackup backup = new SqlceBackup
                {
                    sqlceBackup_id = new Guid("56beaa4c-19af-40bc-975f-3d9150889f36")
                };

                activity = new Activity
                {
                    activity_id = new Guid("13f9fb39-c78b-4c39-82d0-649a3a8448f8"),
                    activity_executionOrder = 3,
                    activity_preWaitingTimeInSeconds = 1,
                    activity_postWaitingTimeInSeconds = 1,
                    activity_activityTypeEnum = ActivityTypeEnum.SqlCeBackup,
                    activity_sqlCeBackup = backup
                };

                activities.Add(activity);


                /*************************************
                * SqlServerCheckData
                ************************************/
                SqlServerCheckData sqlServerCheckData = new SqlServerCheckData
                {
                    sqlServerCheckData_id = new Guid("102d3cbc-992b-4737-8a1f-4363cccd6192"),
                    sqlServerCheckData_sqlQuery = "SELECT Name FROM Customer WHERE Id = @Id ;",
                    sqlServerCheckData_expectedValue = Encoding.UTF8.GetBytes("Bob"),
                    sqlServerCheckData_sqlValueTypeEnum = SqlValueTypeEnum.Nvarchar
                };

                activity = new Activity
                {
                    activity_id = new Guid("e029fe03-8d5c-480c-af46-c499ff8299a5"),
                    activity_executionOrder = 4,
                    activity_preWaitingTimeInSeconds = 6,
                    activity_postWaitingTimeInSeconds = 2,
                    activity_activityTypeEnum = ActivityTypeEnum.SqlServerCheckData,
                    activity_sqlServerCheckData = sqlServerCheckData
                };

                activities.Add(activity);

                /*************************************
                * SqlCeCheckData
                ************************************/
                SqlceCheckData sqlCeCheckData = new SqlceCheckData
                {
                    sqlceCheckData_id = new Guid("bb72e845-6d95-4e5a-8f31-b9933565f56f"),
                    sqlceCheckData_sqlQuery = "SELECT Address FROM Customer Where ID = 345 ;",
                    sqlceCheckData_expectedValue = Encoding.UTF8.GetBytes("186 Fleet Street"),
                    sqlceCheckData_sqlValueTypeEnum = SqlValueTypeEnum.Nvarchar
                };

                activity = new Activity
                {
                    activity_id = new Guid("400c55ab-df3f-4fc8-9f59-5bf15202bb62"),
                    activity_executionOrder = 5,
                    activity_preWaitingTimeInSeconds = 2,
                    activity_postWaitingTimeInSeconds = 1,
                    activity_activityTypeEnum = ActivityTypeEnum.SqlCeCheckData,
                    activity_sqlCeCheckData = sqlCeCheckData
                };

                activities.Add(activity);

                /*************************************
                * MefExecution
                ************************************/
                MefExecution mefExecution = new MefExecution
                {
                    mefExecution_id = new Guid("9285150f-4f16-45c1-b931-b5644cb9e304"),
                    mefExecution_mefComponentDirectory = @"C:\Zen4Sync\TestServer\MEF\MyMefComponent",
                    mefExecution_metadata = "<Customers><Customer id=\"345\"><Address>186 Fleet Street</Address></Customer></Customers>"
                };

                activity = new Activity
                {
                    activity_id = new Guid("fd0e830c-d5e0-4de8-a0f6-380f0555de84"),
                    activity_executionOrder = 6,
                    activity_preWaitingTimeInSeconds = 1,
                    activity_postWaitingTimeInSeconds = 1,
                    activity_activityTypeEnum = ActivityTypeEnum.MefExecution,
                    activity_mefExecution = mefExecution
                };

                activities.Add(activity);

                /*************************************
                * SyncSession WITH overriding data (see its context for the overriding data)
                ************************************/
                activity = new Activity
                {
                    activity_id = new Guid("db381c44-ce99-49b0-9278-f5b19601df07"),
                    activity_executionOrder = 7,
                    activity_preWaitingTimeInSeconds = 1,
                    activity_postWaitingTimeInSeconds = 1,
                    activity_activityTypeEnum = ActivityTypeEnum.SyncSession,
                    activity_syncSession = TypicalSyncSession
                };

                activities.Add(activity);

                /*************************************
                * SyncSession WITHOUT overriding data
                ************************************/
                activity = new Activity
                {
                    activity_id = new Guid("115d83cd-26b6-4baa-b80b-21a1807ee77b"),
                    activity_executionOrder = 8,
                    activity_preWaitingTimeInSeconds = 2,
                    activity_postWaitingTimeInSeconds = 2,
                    activity_activityTypeEnum = ActivityTypeEnum.SyncSession,
                    activity_syncSession = TypicalSyncSession
                };

                activities.Add(activity);

                return activities;
            }
        }

        /// <summary>
        /// A list of ActivityContext containing all the contexts used by the activities present in the ClientTestSessionDescriptor.xml file.
        /// </summary>
        private List<ActivityContext> Contexts
        {
            get
            {
                List<ActivityContext> contexts = new List<ActivityContext>();
                ActivityContext activityContext = null;

                /*************************************
                * SqlServerExecution
                ************************************/
                List<SqlServerExecutionParameter> sqlServerExecutionParameters = new List<SqlServerExecutionParameter>();
                sqlServerExecutionParameters.Add(new SqlServerExecutionParameter {sqlServerExecutionParameter_name = "@Name", sqlServerExecutionParameter_value = Encoding.UTF8.GetBytes("Bob"), sqlServerExecutionParameter_sqlValueTypeEnum = SqlValueTypeEnum.Nvarchar});
                sqlServerExecutionParameters.Add(new SqlServerExecutionParameter { sqlServerExecutionParameter_name = "@Id", sqlServerExecutionParameter_value = Encoding.UTF8.GetBytes("120"), sqlServerExecutionParameter_sqlValueTypeEnum = SqlValueTypeEnum.Int });

                SqlServerExecutionContext sqlServerExecutionContext = new SqlServerExecutionContext()
                {
                    sqlServerExecutionContext_id = new Guid("d49b1001-b88b-4c25-b8d5-dc5539cf7d2f"),
                    sqlServerExecutionContext_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    sqlServerExecutionContext_parameters = sqlServerExecutionParameters  
                };

                activityContext = new ActivityContext()
                {
                    activityContext_id = new Guid("5792a878-99a9-45c2-a7e2-746b30aa89c8"),
                    activityContext_activityId = new Guid("aac891bb-0f67-4b55-b108-37985e4dfb8d"),
                    activityContext_sqlServerExecutionContext = sqlServerExecutionContext
                    
                };

                contexts.Add(activityContext);

                /*************************************
                * SqlCeExecution
                ************************************/
                List<SqlceExecutionParameter> sqlCeExecutionParameters = new List<SqlceExecutionParameter>();
                sqlCeExecutionParameters.Add(new SqlceExecutionParameter { sqlceExecutionParameter_name = "@Name", sqlceExecutionParameter_value = Encoding.UTF8.GetBytes("Nicolas"), sqlceExecutionParameter_sqlValueTypeEnum = SqlValueTypeEnum.Nvarchar });

                SqlceExecutionContext sqlCeExecutionContext = new SqlceExecutionContext
                {
                    sqlceExecutionContext_id = new Guid("a2b7cae5-af23-4e7f-9fda-9693c42f8141"),
                    sqlCeExecutionContext_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    sqlceExecutionContext_parameters = sqlCeExecutionParameters
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("90c79807-473e-4efd-97d7-c9b66b4df6f3"),
                    activityContext_activityId = new Guid("ffb1a3d4-f5a7-41b1-8006-c7477c835c36"),
                    activityContext_sqlCeExecutionContext = sqlCeExecutionContext
                };

                contexts.Add(activityContext);

                /*************************************
                * SqlCeBackup
                ************************************/
                SqlceBackupContext sqlCeBackupContext = new SqlceBackupContext
                {
                    sqlceBackupContext_id = new Guid("e846d2ee-4aee-454a-bfc4-a0205627aa6d"),
                    sqlceBackupContext_sdfFileName = "MyDB_A.sdf",
                    sqlceBackupContext_sdfFileDirectory = "C:"
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("3a431715-df84-45b2-9df7-44a6cab4e336"),
                    activityContext_activityId = new Guid("13f9fb39-c78b-4c39-82d0-649a3a8448f8"),
                    activityContext_sqlCeBackupContext = sqlCeBackupContext
                };

                contexts.Add(activityContext);

                /*************************************
                * SqlServerCheckData
                ************************************/
                List<SqlServerCheckDataParameter> sqlServerCheckDataParameters = new List<SqlServerCheckDataParameter>();
                sqlServerCheckDataParameters.Add(new SqlServerCheckDataParameter { sqlServerCheckDataParameter_name = "@Id", sqlServerCheckDataParameter_value = Encoding.UTF8.GetBytes("120"), sqlServerCheckDataParameter_sqlValueTypeEnum = SqlValueTypeEnum.Int });

                SqlServerCheckDataContext sqlServerCheckDataContext = new SqlServerCheckDataContext
                {
                    sqlServerCheckDataContext_id = new Guid("4a4e6bc4-601a-49c9-a637-b8e42b55338b"),
                    sqlServerCheckDataContext_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    sqlServerCheckDataContext_parameters = sqlServerCheckDataParameters
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("33717460-7b3f-4100-81aa-cf46f2e3f7bb"),
                    activityContext_activityId = new Guid("e029fe03-8d5c-480c-af46-c499ff8299a5"),
                    activityContext_sqlServerCheckDataContext = sqlServerCheckDataContext
                };

                contexts.Add(activityContext);

                /*************************************
                * SqlCeCheckData
                ************************************/
                SqlceCheckDataContext sqlCeCheckDataContext = new SqlceCheckDataContext
                {
                    sqlceCheckDataContext_id = new Guid("131fc101-2c1b-4159-a17a-027d4bebf7bf"),
                    sqlceCheckDataContext_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    sqlceCheckDataContext_parameters = new List<SqlceCheckDataParameter>()
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("6da7a7c4-6765-42fa-850b-d28866fd37b1"),
                    activityContext_activityId = new Guid("400c55ab-df3f-4fc8-9f59-5bf15202bb62"),
                    activityContext_sqlCeCheckDataContext = sqlCeCheckDataContext
                };

                contexts.Add(activityContext);

                /*************************************
                * MefExecution
                ************************************/
                MefExecutionContext mefExecutionContext = new MefExecutionContext
                {
                    mefExecutionContext_id = new Guid("f722506f-63ac-4539-9fe3-2f5e1ada04bf"),
                    mefExecutionContext_metadata = "<Employee id=\"LKS-76\" />"
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("589b829f-dab1-426e-8f9e-4d4555724836"),
                    activityContext_activityId = new Guid("fd0e830c-d5e0-4de8-a0f6-380f0555de84"),
                    activityContext_mefExecutionContext = mefExecutionContext
                };

                contexts.Add(activityContext);

                /*************************************
                * SyncSession WITH overriding data
                ************************************/
                Subscriber subscriber1 = new Subscriber
                {
                    subscriber_id = new Guid("7bae1c71-92ff-44b2-aea4-85d579f2b47b"),
                    subscriber_validateTypeEnum = ValidateTypeEnum.NoValidation,
                    subscriber_name = "First Subscriber",
                    subscriber_hostName = "MDT#1",
                    subscriber_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    subscriber_deleteSdfIfItExists = true,
                    subscriber_sdfFileName = "MyDB_A.sdf",  
                    subscriber_sdfFileDirectory = "C:",
                    subscriber_reinitializeSubscription = true,
                    subscriber_uploadBeforeReinitialize = false
                };

                SyncSessionContext syncSessionContext1 = new SyncSessionContext
                {
                    syncSessionContext_id = new Guid("fb3d3177-dec2-4679-ae48-6fb8f6d16bc2"),
                    syncSessionContext_subscriber = subscriber1
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("68af4480-309a-4c40-8b07-593c35501c33"),
                    activityContext_activityId = new Guid("db381c44-ce99-49b0-9278-f5b19601df07"),
                    activityContext_syncSessionContext = syncSessionContext1,
                    activityContext_overridingData = XElement.Load(Path.Combine(testDeploymentDirectory, @"TestData\OverridingData.xml")).ToString()
                };

                contexts.Add(activityContext);

                /*************************************
                * SyncSession WITHOUT overriding data
                ************************************/
                Subscriber subscriber2 = new Subscriber
                {
                    subscriber_id = new Guid("7bae1c71-92ff-44b2-aea4-85d579f2b47b"),
                    subscriber_validateTypeEnum = ValidateTypeEnum.NoValidation,
                    subscriber_name = "First Subscriber",
                    subscriber_hostName = "MDT#1",
                    subscriber_connectionString = @"DataSource=C:\MyDB_A.sdf",
                    subscriber_deleteSdfIfItExists = true,
                    subscriber_sdfFileName = "MyDB_A.sdf",
                    subscriber_sdfFileDirectory = "C:",
                    subscriber_reinitializeSubscription = true,
                    subscriber_uploadBeforeReinitialize = false
                };

                SyncSessionContext syncSessionContext2 = new SyncSessionContext
                {
                    syncSessionContext_id = new Guid("fb3d3177-dec2-4679-ae48-6fb8f6d16bc2"),
                    syncSessionContext_subscriber = subscriber2
                };

                activityContext = new ActivityContext
                {
                    activityContext_id = new Guid("610a4c1c-bbee-416f-9b3a-d9ee46fa578f"),
                    activityContext_activityId = new Guid("115d83cd-26b6-4baa-b80b-21a1807ee77b"),
                    activityContext_syncSessionContext = syncSessionContext2
                };

                contexts.Add(activityContext);

                return contexts;
            }
        }

        private SyncSession TypicalSyncSession
        {
            get
            {
                Publisher publiser = new Publisher
                {
                    publisher_id = new Guid("d86b3a0b-098c-4821-96cd-91d7892b7fba"),
                    publisher_networkTypeEnum = NetworkTypeEnum.TcpIpSockets,
                    publisher_securityTypeEnum = SecurityTypeEnum.NTAuthentication,
                    publisher_name = "Main Publisher",
                    publisher_address = "http://www.company.com/sqlmergesync/publisher",
                    publisher_database = "CustomersRepository",
                    publisher_login = "GenericEmployee",
                    publisher_password = "GenericPassword"
                };

                Distributor distributor = new Distributor
                {
                    distributor_id = new Guid("075061ad-8142-4af6-8a08-c8024acbe9d1"),
                    distributor_networkTypeEnum = NetworkTypeEnum.DefaultNetwork,
                    distributor_securityTypeEnum = SecurityTypeEnum.DBAuthentication,
                    distributor_name = "Main Distributor",
                    distributor_address = "http://www.company.com/sqlmergesync/distributor",
                    distributor_login = "GenericEmployee",
                    distributor_password = "GenericPassword",
                    distributor_profileName = "GenericProfileName"
                };

                Internet internet = new Internet
                {
                    internet_id = new Guid("c9ecac19-9771-489b-9a36-c80e94f03acc"),
                    internet_name = "Main Company IIS Server",
                    internet_url = "http://www.company.com/sqlmergesync/iis",
                    internet_login = "GenericEmployee",
                    internet_password = "GenericPassword",
                    internet_proxyServer = "210.34.54.98",
                    internet_proxyLogin = "sdpwo34s",
                    internet_proxyPassword = ";mjd@!fgr"
                };

                SyncSession syncSession = new SyncSession
                {
                    syncSession_id = new Guid("bff68c6d-23fa-4c75-86c8-d6151a73e4bb"),
                    syncSession_exchangeTypeEnum = ExchangeTypeEnum.BiDirectional,
                    syncSession_snapshotTransferTypeEnum = SnapshotTransfertTypeEnum.FTP,
                    syncSession_compressionLevel = 2,
                    syncSession_connectionRetryTimeout = 1,
                    syncSession_connectTimeout = 60,
                    syncSession_hostName = "MyGenericHostName",
                    syncSession_loginTimeout = 30,
                    syncSession_postSyncCleanup = 0,
                    syncSession_publication = "AllCustomers",
                    syncSession_queryTimeout = 30,
                    syncSession_receiveTimeout = 30,
                    syncSession_sendTimeout = 30,
                    syncSession_publisher = publiser,
                    syncSession_distributor = distributor,
                    syncSession_internet = internet
                };

                return syncSession;
            }
        }
    }
}
    